package com.zhengtoudai.base;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zhengtoudai.util.TitleUtil;
import com.zhengtoudai.view.statusView.StatusView;
import com.zhengtoudai.view.swipetoloadlayout.base.SwipeToLoadLayout;


/**
 * 作者：黑哥 on 2016/3/29 09:54
 */
public abstract class BaseFragment extends Fragment {

    //对外提供一个title的方法
    protected TitleUtil titleUtil;

    protected String TAG=this.getClass().getSimpleName();

    protected BaseActivity activity;
    protected Context mContext;
    //调用下面方法显示布局
    private StatusView status;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    protected   View rootView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity= (BaseActivity) getActivity();
        mContext=getContext();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return super.onCreateView(inflater,container,savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        this.rootView=view;
        titleUtil=new TitleUtil(activity,view);
        //getChildFragmentManager().beginTransaction().add(view.getId(),new ProjectFragment());
        initTitle();
        initView();
        setData();

    }
    //记录原来view的状态
    private int paddingLeft,paddingTop,paddingRight,paddingBottom;
    protected void initStatus(){
        if (status==null){
            status=new StatusView(mContext);
            status.hintLineView();
        }
        if (status.getParent()==null){
            final ViewGroup group= (ViewGroup) rootView;
            //保存参数状态
            paddingLeft=group.getPaddingLeft();
            paddingTop=group.getPaddingTop();
            paddingRight=group.getPaddingRight();
            paddingBottom=group.getPaddingBottom();

            group.setPadding(0, 0, 0, 0);
            group.post(new Runnable() {
                @Override
                public void run() {

                    ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(group.getWidth(), group.getHeight());
                    for (int i = 0; i < group.getChildCount(); i++) {
                        group.getChildAt(i).setVisibility(View.GONE);
                    }
                    if (status.getParent()!=null){
                        status.removeAttachView();
                    }
                    group.addView(status, layoutParams);
                }
            });

        }

    }

    /**
     * 清除状态布局
     */
    protected void removeStatus(){
        if (status==null){
            return;
        }
        status.removeAttachView();
        ViewGroup group= (ViewGroup) rootView;
        //rootView.setLayoutParams(oldParams);
        for (int i=0;i<group.getChildCount();i++){
            group.getChildAt(i).setVisibility(View.VISIBLE);
        }
        group.setPadding(paddingLeft,paddingTop,paddingRight,paddingBottom);
    }
    /**
     * 全屏显示无数据状态,无点击事件
     */
    protected void showNoData(String message){
        initStatus();
        status.NoData(message);
    }
    /**
     * 全屏显示无数据状态,有点击事件
     */
    protected void showNoData( String message,  String btnText, View.OnClickListener listener){
        initStatus();
        status.NoData(message,btnText,listener);
    }

    /**
     * 全屏显示无网络状态,有点击事件
     * @param listener
     */
    protected void showNoNewWork(View.OnClickListener listener){
        initStatus();
        status.NoNewWork(listener);
    }

    /**
     * 全屏显示加载中状态
     */
    protected void showLoading(){
        initStatus();
        status.Loading();
    }
    //需要时重新  一般fragment是不需要title的
    protected  void initTitle(){

    }
    protected  void initView(){

    }
    protected  void setData(){

    }

    protected void onComplete(SwipeToLoadLayout refresh){
        if (refresh!=null){
            if (refresh.isRefreshing()){
                refresh.setRefreshing(false);
            }else if (refresh.isLoadingMore()){
                refresh.setLoadingMore(false);
            }
        }

    }
    protected void autoLoading(final SwipeToLoadLayout refresh,RecyclerView swipeTarget){
        //自动上拉
        swipeTarget.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (!ViewCompat.canScrollVertically(recyclerView, 1)) {
                        refresh.setLoadingMore(true);
                    }
                }
            }
        });
    }

    public void startActivity(Class<?> cls) {
        Intent intent = new Intent(activity, cls);
        startActivity(intent);
    }

    public void startActivity(Bundle bundle, Class<?> cls) {
        Intent intent = new Intent(activity, cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     *
     * @param message  提示信息
     * @param btnNegative  取消按钮
     * @param btnPositive  确认按钮
     * @param positiveListener 确认按钮点击事件
     * @return
     */
    protected AlertDialog.Builder showAlertDialog(String message, String btnNegative, String btnPositive, Dialog.OnClickListener positiveListener){
        final AlertDialog.Builder builder=new AlertDialog.Builder(mContext);
        builder.setTitle("温馨提示");
        builder.setMessage(message);
        builder.setNegativeButton(btnNegative, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setPositiveButton(btnPositive,positiveListener);
        builder.show();
        return  builder;
    }
    private ProgressDialog mProgressDialog;
    protected ProgressDialog showProgressDialog(String message){
        if (mProgressDialog==null){
            mProgressDialog=new ProgressDialog(mContext);
            mProgressDialog.setCancelable(false);
            mProgressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (event.getAction()==KeyEvent.ACTION_DOWN&&keyCode==KeyEvent.KEYCODE_BACK){
                        dialog.dismiss();
                        return true;
                    }
                    return false;
                }
            });
        }
        mProgressDialog.setMessage(message);
        mProgressDialog.show();
        return mProgressDialog;
    }

    protected void dismissProgressDialog(){
        if (mProgressDialog!=null){
            mProgressDialog.dismiss();
        }
    }

}
